Spring Boot Data Redis
๐Ÿ™Š

Spring Boot Data Redis

Created
Jul 16, 2024 07:00 AM
Last edited time
Last updated July 17, 2024
Tags
Backend
Spring
Language
Java
URL

Intro::

Spring Boot Data Redis ์—ฐ๊ฒฐ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ์ •๋ฆฌ๋ณธ์ž…๋‹ˆ๋‹ค.
ย 

Config ํŒŒ์ผ ์„ค์ •

// RedisConfig @Configuration public class RedisConfig { @Value("${spring.data.redis.host}") private String host; @Value("${spring.data.redis.password}") private String password; @Value("${spring.data.redis.port}") private int port; @Bean public RedisConnectionFactory redisConnectionFactory() { // RedisClusterConfiguration() RedisStandaloneConfiguration redisConfig = new RedisStandaloneConfiguration(); redisConfig.setHostName(host); redisConfig.setPassword(password); redisConfig.setPort(port); return new LettuceConnectionFactory(redisConfig); } @Bean public RedisTemplate<String, Object> redisTemplate() { RedisTemplate<String, Object> template = new RedisTemplate<>(); template.setConnectionFactory(redisConnectionFactory()); template.setKeySerializer(new StringRedisSerializer()); template.setValueSerializer(new GenericJackson2JsonRedisSerializer()); return template; } }
  • LettuceConnectionFactory
    • RedisStandaloneConfiguration (๋‹จ์ผ ์ธ์Šคํ„ด์Šค ์„ค์ •)
    • RedisSentinelConfiguration (Sentinel ์„ค์ •)
    • RedisClusterConfiguration (ํด๋Ÿฌ์Šคํ„ฐ ์„ค์ •)
  • RedisTemplate
    • Redis์˜ ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ์„ ์„ธ๋ฐ€ํ•˜๊ฒŒ ์ œ์–ดํ•˜๊ณ , ๊ณ ๊ธ‰ ์—ฐ์‚ฐ ๋ฐ ๋ณต์žกํ•œ ์ฟผ๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•  ๋•Œ ์ ํ•ฉํ•ฉ๋‹ˆ๋‹ค. ์œ ์—ฐ์„ฑ๊ณผ ์„ฑ๋Šฅ ์ตœ์ ํ™”๊ฐ€ ์ค‘์š”ํ•œ ๊ฒฝ์šฐ์— ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค.
ย 

๋ ˆ๋””์Šค ์ ‘๊ทผ ๋ฐฉ๋ฒ•

@Autowired RedisTemplate<String, Object> redisTemplate; ValueOperations<String, Object> ops= redisTemplate.opsForValue(); // or @Resource(name="redisTemplate") private ValueOperations<String, Object> ops; redisTemplate.hasKey(key);// ํ‚ค ํ™•์ธ ops.set(key, object);// ์ €์žฅ ops.get(key);// value ๊ฐ€์ ธ์˜ค๊ธฐ
ย 

References::

Loading Comments...